package com.ccb.health.controller;

import com.ccb.health.entity.HealthInfo;
import com.ccb.health.entity.User;
import com.ccb.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    private String prefix = "/user";

    @Autowired
    private UserService userService;

    /**
     * 跳转到用户首页
     * @return
     */
    @GetMapping("/user")
    public String user(){
        return prefix + "/user";
    }

    /**
     * 跳转到编辑用户信息界面
     * @return
     */
    @GetMapping("/editInfo")
    public String editInfo(ModelMap mmap, HttpSession session){
        return prefix + "/editInfo";
    }

    /**
     * 保存用户修改信息
     * @param user
     * @return
     */
    @PostMapping("/editInfo")
    public String editInfoSave(User user, ModelMap mmap, HttpSession session){
        int i = userService.updateUser(user);
        User user0 = (User) session.getAttribute("user");
        session.removeAttribute("user");
        user.setId(user0.getId());
        user = userService.selectUser(user).get(0);
        session.setAttribute("user", user);
        return "redirect:/viewHistory";
    }

    /**
     * 返回用户当天的登记信息
     * @return
     */
    @GetMapping("/editHealthInfo")
    public String editHealthInfo(ModelMap mmap, HttpSession session){
        User user = (User) session.getAttribute("user");
        HealthInfo healthInfo = userService.selectHealthToday(user.getId());
        if(healthInfo != null){
            mmap.addAttribute("healthInfo", healthInfo);
        }
        return prefix + "/editHealthInfo";
    }

    /**
     * 保存用户当天的登记信息
     * @return
     */
    @PostMapping("/editHealthInfo")
    public String editHealthInfoSave(HealthInfo healthInfo, ModelMap mmap, HttpSession session){
        User user = (User) session.getAttribute("user");
        HealthInfo healthInfo0 = userService.selectHealthToday(user.getId());
        if(healthInfo0 == null){
            // 直接插入
            healthInfo.setTime(new Date());
            userService.insertHealth(healthInfo);
        }else{
            // 更新数据
            healthInfo.setTime(new Date());
            userService.updateHealth(healthInfo);
        }
        return "redirect:/viewHistory";
    }

    /**
     * 返回登记历史界面
     * @return
     */
    @GetMapping("/viewHistory")
    public String viewHistory(){
        return prefix + "/viewHistory";
    }

    /**
     * 返回用户登记历史记录查看
     * @return
     */
    @PostMapping("/viewHistory")
    @ResponseBody
    public Map<String, Object> viewHistory(HttpSession session){
        User user = (User) session.getAttribute("user");
        List<HealthInfo> res = userService.selectHistoryHealth(user.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("rows", res);
        map.put("code", 0);
        map.put("total", res.size());
        return map;
    }
}
